﻿<%@ Page Language="C#" MasterPageFile="~/NestedMasterPage.master" %>

<%@ Register TagPrefix="Page" TagName="Nav" Src="~/UserControls/PageControl.ascx" %>
<%@ Register TagPrefix="Page" TagName="SiteMap" Src="~/UserControls/SiteMapControl.ascx" %>

<%@ Import Namespace="System.Web.Script.Serialization" %>

<script runat="server">


  protected void Page_Load(object sender, EventArgs e)
  {
    if (Page.IsPostBack == false)
    {
      //初始化檢視筆數的顏色為第一筆
      this.LinkButton1.CssClass = "current";
      //初始化查詢數量與排序
      Session["ListCount"] = null;
      Session["IsOrderAesc"] = null;
      //產生清單
      GenProductList();
    }
  }

  private void GenProductList(string listCount = "15", string isOrderAesc = "0")
  {
    string Prod_Cat_ID = System.Configuration.ConfigurationManager.AppSettings["Period"].ToString();
    SiteMapControl.Prod_Cat_ID = Prod_Cat_ID;

    string ListCount = Session["ListCount"] == null ? listCount : Session["ListCount"].ToString();
    string IsOrderAesc = Session["IsOrderAesc"] == null ? isOrderAesc : Session["IsOrderAesc"].ToString();
    int PageNumber = this.Nav1.currentPageNumber;
    int StartNumber = 1 + Convert.ToInt32(ListCount) * (PageNumber - 1);
    int EndNumber = StartNumber + Convert.ToInt32(ListCount) - 1;

    B2C_MALL_GetProductList_4 ws = new B2C_MALL_GetProductList_4();
    ws.Url = ConfigurationManager.AppSettings["B2C_MALL_GetProductList_4"].ToString(); 
    string jsonString = ws.B2C_MALL_GetProductList(
        System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
        , HttpContext.Current.User.Identity.Name, Prod_Cat_ID, StartNumber.ToString(), EndNumber.ToString(), IsOrderAesc, MinAmount.Value, MaxAmount.Value);
    JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);

    string Is_Valid = restoredObject["Is_Valid"].ToString();
    string msg = restoredObject["Msg"].ToString();

    if (Is_Valid == "Y")
    {
      dynamic dynObj = restoredObject["ProductList_Ary"];      
      this.ListView1.DataSource = dynObj;
      this.ListView1.DataBind();
      this.Nav1.TotalCount = this.Nav2.TotalCount = restoredObject["Total_Count"].ToString();
      this.Nav1.PagesCount = this.Nav2.PagesCount =
          Math.Ceiling((Convert.ToDecimal(restoredObject["Total_Count"]) / Convert.ToDecimal(ListCount))).ToString();
    }
    else
    {
      this.ListView1.DataSource = null;
      this.ListView1.DataBind();
      this.Nav1.TotalCount = this.Nav2.TotalCount = "0";
      this.Nav1.PagesCount = this.Nav2.PagesCount = "0";
    }
    //不滿5頁的去除其頁數
    this.Nav1.DisplayPageNumber();
    this.Nav2.DisplayPageNumber();
  }

  protected void LinkButton2_Click(object sender, EventArgs e)
  {
    //將檢視筆數的顏色先清除
    foreach (Control item in this.numsPlaceHolder.Controls)
    {
      if (item is LinkButton)
      {
        ((LinkButton)item).CssClass = "";
      }
    }

    //從第一頁開始
    this.Nav1.currentPageNumber = 1;
    this.Nav2.currentPageNumber = 1;

    //將postback的檢視筆數設定顏色        
    LinkButton selectButton = ((LinkButton)sender);
    selectButton.CssClass = "current";

    //存Session
    Session["ListCount"] = selectButton.Text;

    //產生產品清單
    GenProductList(selectButton.Text);

  }

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    //存Session
    Session["IsOrderAesc"] = DropDownList1.SelectedValue;

    //取得排序條件並產生產品清單 (0:預設1:正向2:反向)
    GenProductList(isOrderAesc: DropDownList1.SelectedValue);
  }

  protected void Nav1_OnPageSelected(object sender)
  {
    if (sender == this.Nav1)
    {
      this.Nav2.currentPageNumber = this.Nav1.currentPageNumber;
    }
    else
    {
      this.Nav1.currentPageNumber = this.Nav2.currentPageNumber;
    }
    //取得欲檢視頁面號碼並產生產品清單 
    GenProductList();
  }

  protected void lb_search_Click(object sender, EventArgs e)
  {
    GenProductList();
  }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" runat="server">
  <div class="simple-banner">
    <img id="imgBanner" src="~/Images/banners/SimpleBanner/Simple5.jpg" alt="期間限定" runat="server" />
  </div>
  <div class="breadcrumbs">
    <Page:SiteMap runat="server" ID="SiteMapControl" />    
  </div>

  <div class="filter-area">
    <div class="filter-left">
      <div class="slide-filter">
        <p>
          價位區間 : 
          <span id="amount">$0 - $20000 以上</span>
          <asp:LinkButton ID="lb_search" runat="server" CssClass="btn fr" Text="確 定" OnClick="lb_search_Click" />
          <asp:HiddenField ID="MinAmount" runat="server" ClientIDMode="Static" />
          <asp:HiddenField ID="MaxAmount" runat="server" ClientIDMode="Static" />
        </p>
        <div class="slider-range"></div>
        <script src="../Scripts/jquery-ui-1.10.3.custom.min.js"></script>
        <script>
          $(".slider-range").slider({
            range: true,
            min: 0,
            max: 20000,
            step: 100,
            values: [0, 20000],
            slide: function (event, ui) {
              if (ui.values[1] == 20000) {
                $("#amount").text("$" + ui.values[0] + " - $" + ui.values[1] + " 以上");
                $("#MaxAmount").val("999999");
              }
              else {
                $("#amount").text("$" + ui.values[0] + " - $" + ui.values[1]);
                $("#MaxAmount").val(ui.values[1]);
              }
              $("#MinAmount").val(ui.values[0]);
            }
          });

          //C# Page_Load
          if ($("#MinAmount").val() != "" && $("#MaxAmount").val() != "") {
            $(".slider-range").slider("values", 0, $("#MinAmount").val());
            $(".slider-range").slider("values", 1, $("#MaxAmount").val());
            if ($("#MaxAmount").val() >= 20000) {
              $("#amount").text("$" + $("#MinAmount").val() + " - $20000 以上");
            }
            else {
              $("#amount").text("$" + $("#MinAmount").val() + " - $" + $("#MaxAmount").val());
            }
          }
        </script>
      </div>
    </div>
    <div class="filter-mid">
      <div class="drop-filter">
        排序方式               
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
          <asp:ListItem Value="0">請選擇</asp:ListItem>
          <asp:ListItem Value="1">價格由低到高</asp:ListItem>
          <asp:ListItem Value="2">價格由高到低</asp:ListItem>
        </asp:DropDownList>
      </div>
    </div>
    <div class="filter-right">
      <div class="num-filter">
        每頁顯示商品數  
        <div class="nums">
          <asp:PlaceHolder ID="numsPlaceHolder" runat="server">
            <%--<a href="#" class="current">15</a> / <a href="#">30</a> / <a href="#">60</a>--%>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton2_Click">15</asp:LinkButton>
            / 
                <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">30</asp:LinkButton>
            / 
                <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton2_Click">60</asp:LinkButton>
          </asp:PlaceHolder>
        </div>
      </div>
    </div>
  </div>

  <!-- Page Nav User Control -->
  <Page:Nav ID="Nav1" runat="server" OnOnPageSelected="Nav1_OnPageSelected" /> 

  <asp:ListView ID="ListView1" runat="server">
    <LayoutTemplate>
      <div id="itemPlaceholderContainer" runat="server" class="product-list">
        <li runat="server" id="itemPlaceholder" />
      </div>
    </LayoutTemplate>
    <ItemTemplate>
      <div class="product-item">
        <div class="item-image">
          <a id="A1" href='<%# Server.HtmlEncode(Eval("B2C_CODE","~/Products/ProductDetail.aspx?B2C_CODE={0}").ToString()) %>' runat="server">
            <img id="Img1" src='<%# Server.HtmlEncode(Eval("IMAGE_PATH_M").ToString()) %>' runat="server" />
          </a>
        </div>
        <div class="release">
          <%# CommonUtil.GetNEWAndHOT(Server.HtmlEncode(Eval("light_status").ToString())) %>
        </div>
        <div class="description">
          <div class="desc-top">
            <p>期間限定價 $ <strong><%# Server.HtmlEncode(Eval("RangeDscPrice","{0:N0}").ToString()) %></strong></p>
            <p><b><%# Server.HtmlEncode(Eval("START_DATE","{0:d}").ToString()) %> ~ 
              <%# Server.HtmlEncode(Eval("END_DATE","{0:d}").ToString()) %></b></p>
          </div>
          <div class="desc-bottom">
            <p><%# Server.HtmlEncode(Eval("B2C_CODE_NAME").ToString()) %></p>
           <%-- <p>非期間限定特價$ <%# Server.HtmlEncode(Eval("ACT_PRICE","{0:N0}").ToString()) %></p>--%>
          </div>
        </div>
      </div>
    </ItemTemplate>
  </asp:ListView>
  <!-- Page Nav User Control -->
  <Page:Nav ID="Nav2" runat="server" OnOnPageSelected="Nav1_OnPageSelected" />
  <div class="go-to-top">
    <a href="#">回到頂端</a>
  </div>
</asp:Content>
